package com.ruoyi.customerapply.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.customer.domain.CrmCustomer;
import com.ruoyi.customer.enums.CrmCustomerStatusType;
import com.ruoyi.customerapply.enums.CustomerapplyStatusType;
import com.ruoyi.customermerchants.domain.CrmCustomerMerchants;
import com.ruoyi.customermerchants.enums.MerchantsStatusType;
import com.ruoyi.customermerchants.mapper.CrmCustomerMerchantsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.customerapply.mapper.CrmCustomerApplyMapper;
import com.ruoyi.customerapply.domain.CrmCustomerApply;
import com.ruoyi.customerapply.service.ICrmCustomerApplyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户申请Service业务层处理
 * 
 * @author hct
 * @date 2022-02-16
 */
@Service
public class CrmCustomerApplyServiceImpl implements ICrmCustomerApplyService 
{
    @Autowired
    private CrmCustomerApplyMapper crmCustomerApplyMapper;
    //销售线索
    @Autowired
    private CrmCustomerMerchantsMapper crmCustomerMerchantsMapper;

    /**
     * 查询客户申请
     * 
     * @param id 客户申请主键
     * @return 客户申请
     */
    @Override
    public CrmCustomerApply selectCrmCustomerApplyById(Long id)
    {
        return crmCustomerApplyMapper.selectCrmCustomerApplyById(id);
    }

    /**
     * 查询客户申请列表
     * 
     * @param crmCustomerApply 客户申请
     * @return 客户申请
     */
    @Override
    public List<CrmCustomerApply> selectCrmCustomerApplyList(CrmCustomerApply crmCustomerApply)
    {
        return crmCustomerApplyMapper.selectCrmCustomerApplyList(crmCustomerApply);
    }

    /**
     * 新增客户申请
     * 
     * @param crmCustomerApply 客户申请
     * @return 结果
     */
    @Override
    public int insertCrmCustomerApply(CrmCustomerApply crmCustomerApply)
    {
        return crmCustomerApplyMapper.insertCrmCustomerApply(crmCustomerApply);
    }

    @Override
    @Transactional
    public int agreeCrmCustomerApply(CrmCustomerApply crmCustomerApply)
    {
        if(crmCustomerApply.getStatus().intValue()==1){
            //将申请加入到线索中
            CrmCustomerMerchants crmCustomerMerchants=new CrmCustomerMerchants();
            BeanUtils.copyBeanProp(crmCustomerMerchants,crmCustomerApply);
            crmCustomerMerchants.setStatus(0);
            crmCustomerMerchants.setRegisterTime(new Date());
            crmCustomerMerchants.setSource("自己申请");
            crmCustomerMerchantsMapper.insertCrmCustomerMerchants(crmCustomerMerchants);
        }
        return crmCustomerApplyMapper.updateCrmCustomerApply(crmCustomerApply);
    }
    /**
     * 修改客户申请
     * 
     * @param crmCustomerApply 客户申请
     * @return 结果
     */
    @Override
    public int updateCrmCustomerApply(CrmCustomerApply crmCustomerApply)
    {
        return crmCustomerApplyMapper.updateCrmCustomerApply(crmCustomerApply);
    }

    /**
     * 批量删除客户申请
     * 
     * @param ids 需要删除的客户申请主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerApplyByIds(Long[] ids)
    {
        return crmCustomerApplyMapper.deleteCrmCustomerApplyByIds(ids);
    }

    /**
     * 删除客户申请信息
     * 
     * @param id 客户申请主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerApplyById(Long id)
    {
        return crmCustomerApplyMapper.deleteCrmCustomerApplyById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer customerToMerchants(Long id) {
        Date now =new Date();
        CrmCustomerApply crmCustomerApply= crmCustomerApplyMapper.selectCrmCustomerApplyById(id);
        CrmCustomerMerchants crmCustomerMerchants = crmCustomerMerchantsMapper.selectCrmCustomerMerchantsById(id);
        crmCustomerApply.setStatus(CustomerapplyStatusType.DELETE.getCode());
        int i = crmCustomerApplyMapper.updateCrmCustomerApply(crmCustomerApply);
        CrmCustomerMerchants crmCustomer = new CrmCustomerMerchants();
        crmCustomerMerchants.setCompanyAddress(crmCustomerMerchants.getCompanyAddress());
        crmCustomerMerchants.setCompanyType(crmCustomerMerchants.getCompanyType());
        crmCustomerMerchants.setCompanyName(crmCustomerMerchants.getCompanyName());
        crmCustomerMerchants.setCompanyDesc(crmCustomerMerchants.getRemark());
        crmCustomerMerchants.setEmail(crmCustomerMerchants.getEmail());
        crmCustomerMerchants.setIndustryname(crmCustomerMerchants.getIndustryname());
        crmCustomerMerchants.setIndustryId(crmCustomerMerchants.getIndustryId());
        crmCustomerMerchants.setMobile(crmCustomerMerchants.getMobile());
        crmCustomerMerchants.setName(crmCustomerMerchants.getName());
        crmCustomerMerchants.setPost(crmCustomerMerchants.getPost());
        crmCustomerMerchants.setScale(crmCustomerMerchants.getScale());
        crmCustomerMerchants.setAddress(crmCustomerMerchants.getAddress());
        crmCustomerMerchants.setStatus(CrmCustomerStatusType.DEAL.getCode());
        crmCustomerMerchants.setBusinessScope(crmCustomerMerchants.getBusinessScope());
        crmCustomerMerchants.setCompanystatus(crmCustomerMerchants.getCompanystatus());
        crmCustomerMerchants.setRegisterMoney(crmCustomerMerchants.getRegisterMoney());
        crmCustomerMerchants.setRegisterTime(crmCustomerMerchants.getRegisterTime());
        crmCustomerMerchants.setSocialCreditCode(crmCustomerMerchants.getSocialCreditCode());
        crmCustomerMerchants.setCompanyLogo(crmCustomerMerchants.getCompanyLogo());
        crmCustomerMerchants.setManagementType(crmCustomerMerchants.getManagementType());
        crmCustomerMerchants.setCompanyNature(crmCustomerMerchants.getCompanyNature());
        crmCustomerMerchants.setRegistrationAuthority(crmCustomerMerchants.getRegistrationAuthority());
        crmCustomerMerchants.setLegalPerson(crmCustomerMerchants.getLegalPerson());
        int i1 = crmCustomerMerchantsMapper.insertCrmCustomerMerchants(crmCustomer);
        return i1;
    }

}
